# Maintainer: Zoltan Gyarmati <zgyarmati@zgyarmati.de>

_realname=dwarfstack
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.2
pkgrel=2
pkgdesc="Provides stacktrace with source file/line information (mingw-w64)"
arch=('any')
mingw_arch=( 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/ssbssa/dwarfstack"
license=('spdx:LGPL-2.1-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc")
source=("https://github.com/ssbssa/${_realname}/archive/${pkgver}/${_realname}-${pkgver}.tar.gz.tar.gz"
        "001-aarch64-stack.patch"
        "002-no-int-conversion.patch")
sha256sums=('1fca1d12756941c4c932b50f9abe56a3756f012a1e93deef553e141a78cc2709'
            '424eddaf3bd3dbf68a923592f58ffe7224e4a7e3245be1e2a76bad74326c39c9'
            'c8a22b5d7e5e290f745d292518e85d0b2b3b484c4d011aaaf1faf97291b83883')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 --binary -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd ${_realname}-${pkgver}

  apply_patch_with_msg \
    001-aarch64-stack.patch \
    002-no-int-conversion.patch
}

build() {
  cp -r "${srcdir}/${_realname}-${pkgver}" "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  make CC=${CC}
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  make install PREFIX="${pkgdir}${MINGW_PREFIX}"
  install -D -m644 "${srcdir}/${_realname}-${pkgver}/LICENSE.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
